package ua.exchangeratesukraine.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class Util {

	private final static String	LOG_TAG			= "Util";
	private static String		month_easter	= "";
	private static String		day_easter		= "";
	private static String		month_trinity	= "";
	private static String		day_trinity		= "";

	private static int			sYear			= 0;
	private static int			sMonth			= 0;
	private static int			sDayOfMonth		= 0;
	private static int			setMonth		= 0;

	private static int			currentYear		= 0;
	private static int			currentMonth	= 0;
	private static int			currentDay		= 0;

	static {
		currentYear = Calendar.getInstance().get(Calendar.YEAR);
		currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
		currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);

	}

	public static void calcHoliday(int year) {
		int a = (19 * (year % 19) + 15) % 30;
		int b = (2 * (year % 4) + 4 * (year % 7) + 6 * a + 6) % 7;

		int oldDate = (a + b) > 9 ? (a + b - 9) : (22 + a + b);

		int newDate;
		String newMonth = "";

		int newDateTrinity = 0;
		String newMonthTrinity = "";
		// 4 - april; 5 - may ; 6 - june
		if ((a + b) > 9) { // april
			newDate = oldDate + 13 > 30 ? oldDate + 13 - 30 : oldDate + 13;
			newMonth = oldDate + 13 > 30 ? "5" : "4";

			newDateTrinity = ((oldDate + 13) + 49 > 61) ? ((oldDate + 13) + 19 - 31) : (oldDate + 13) + 19;

			newMonthTrinity = (((oldDate + 13) + 49) > 60) ? "6" : "5";

		} else { // march
			newDate = oldDate + 13 > 31 ? oldDate + 13 - 31 : oldDate + 13;
			newMonth = oldDate + 13 > 31 ? "4" : "3";

			newDateTrinity = ((oldDate + 13) + 49 > 61) ? ((oldDate + 13) + 19 - 31) : (oldDate + 13) + 19;
			newMonthTrinity = (((oldDate + 13) + 49) > 92) ? "6" : "5";
		}

		month_easter = newMonth;
		day_easter = String.valueOf(newDate);
		month_trinity = newMonthTrinity;
		day_trinity = String.valueOf(newDateTrinity);

	}

	public static boolean isDayOff(ArrayList<String> holidayMonthArray, ArrayList<String> holidayDayArray,
			String date) {
		System.out.println("String date : " + date);

		initTime(date);
		Util.calcHoliday(sYear);

		holidayMonthArray.add(month_easter);
		holidayMonthArray.add(month_trinity);
		holidayDayArray.add(day_easter);
		holidayDayArray.add(day_trinity);

		Calendar c = Calendar.getInstance(Locale.getDefault());
		c.set(sYear, setMonth, sDayOfMonth);

		String dayOfWeek = c.getDisplayName(Calendar.DAY_OF_WEEK, 1, java.util.Locale.UK);

		// check off-day
		if (dayOfWeek.equals(Constants.SATURDAY) || dayOfWeek.equals(Constants.SUNDAY)) {

			return true;
		}

		// check off-day transfer (monday after sunday or saturday)
		int tempDaySaturday = sDayOfMonth - 2;
		int tempDaySunday = sDayOfMonth - 1;
		int i = 0;
		if (dayOfWeek.equals(Constants.MONDAY)) {
			for (String holidayMonth : holidayMonthArray) {
				if (sMonth == Integer.valueOf(holidayMonth)
						&& (tempDaySunday == Integer.valueOf(holidayDayArray.get(i)) || tempDaySaturday == Integer
								.valueOf(holidayDayArray.get(i)))) {
					return true;
				}
				i++;
			}
		}
		// check holidays
		int j = 0;
		for (String holidayMonth : holidayMonthArray) {
			if (sMonth == Integer.valueOf(holidayMonth)
					&& sDayOfMonth == Integer.valueOf(holidayDayArray.get(j))) {
				return true;
			}
			j++;
		}
		return false;
	}

	public static void initTime(String date) {
		sYear = Integer.valueOf(date.substring(0, 4));
		sMonth = Integer.valueOf(date.substring(5, 7));
		sDayOfMonth = Integer.valueOf(date.substring(8));
		setMonth = sMonth - 1;
	}

	public static boolean isTimeFuture(String date) {
		initTime(date);
		Log.d(LOG_TAG, "Year : " + currentYear + "; month : " + currentMonth + "; day : " + currentDay);
		if (currentYear < sYear || (currentYear <= sYear && currentMonth < sMonth)
				|| (currentYear <= sYear && currentMonth <= sMonth && currentDay < sDayOfMonth)) {
			return true;
		}
		return false;
	}

	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
		return ((netInfo != null && netInfo.isConnectedOrConnecting()) ? true : false);
	}
	
	public static String longToString(long date) {
		return (new java.sql.Date(date)).toString();
	}

	public static long stringToLong(String inputDate) {
		long mills = 0;
		try {
			mills = ((Date) (new SimpleDateFormat("yyyy-MM-dd")).parse(inputDate)).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return mills;
	}
}